﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
using Arles.src.view.controls;
using ArlesInterfaces.Editor;
using Arles.src.controller;

namespace Arles.src.view
{
    public partial class FHistory : DockContent, IHistory
    {
        GListBox lb;
        int itemIndex;

        #region Properties
        public bool Active { get; set; }

        public int ItemIndex
        {
            get { return itemIndex; }
            set
            {
                itemIndex = value;
                lb.SelectedIndex = itemIndex;
            }
        } 

        #endregion

        public FHistory()
        {
            InitializeComponent();
            lb = new GListBox();
            lb.Dock = DockStyle.Fill;
            lb.ImageList = imageList;
            this.Controls.Add(lb);
            Active = true;
            lb.SelectedValueChanged += new EventHandler(lb_SelectedValueChanged);
        }

        void lb_SelectedValueChanged(object sender, EventArgs e)
        {
            if (ItemIndex != lb.SelectedIndex)
            {
                Active = false;
                GListBoxItem item = ((GListBoxItem)lb.Items[lb.SelectedIndex]);
                if (item.UndoItem)
                {
                    for (int i = 0; i < item.ItemIndex - 1; i++)
                        Editor.Instance.Invoker.Undo();

                    Active = true;
                    Editor.Instance.Invoker.Undo();
                }
                else
                {
                    for (int i = 0; i < item.ItemIndex; i++)
                        Editor.Instance.Invoker.Redo();

                    Active = true;
                    Editor.Instance.Invoker.Redo();
                }
            }
        }

        public void Draw(List<ICommand> undo, List<ICommand> redo)
        {
            if (Active)
            {
                lb.Items.Clear();

                for (int i = undo.Count - 1; i > -1; i--)
                    lb.Items.Add(new GListBoxItem(undo[i].Label, undo[i].ImageIndex, true, i));

                ItemIndex = lb.Items.Count - 1;

                for (int i = 0; i < redo.Count; i++)
                    lb.Items.Add(new GListBoxItem(redo[i].Label, redo[i].ImageIndex, false, i));
            }
        }
    }
}
